package cn.sanshuilin.kst.system.provider.write.dao;

import cn.sanshuilin.kst.common.utils.LogPrinter;
import cn.sanshuilin.kst.system.provider.bean.Information;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;
import java.util.List;

/**
 * @author greenlee
 * @version v1.0
 * @create 创建时间: 2017-12-01 21:40
 * Copyright 2017  by 三水林科技有限公司
 **/
@Repository
public class InformationReq extends AbstractJpaDao<Information> {

    public Object getInformationsCount(String strKeyword) {
        long count = 0;
            Query query = this.getInformationsQuery(" COUNT(*) ", strKeyword);
        return query.getSingleResult();
    }



    public List<Information> getInformations(int nPageIndex, int nPageSize, String strKeyword) {

            Query query = getInformationsQuery("c", strKeyword);

            query.setFirstResult(nPageIndex * nPageSize);
            query.setMaxResults(nPageSize);
        List<Information> list  = query.getResultList();

        return list;
    }

    private Query getInformationsQuery(String strChange, String strKeyword) {
        String hql = "SELECT " + strChange + " FROM Information c WHERE (c.Title LIKE :Keyword ) ";

        hql += " ORDER BY c.Title DESC ";

        Query query =entityManager.createNativeQuery(hql);

        query.setParameter("Keyword", "%" + strKeyword + "%");

        LogPrinter.info("----------------------"+query);

        return query;
    }
}
